/**
 * 冒泡排序，冒泡排序有不使用额外空间只操作数组本身的特点
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */

const swap = (nums, i, j) => {
  const temp = nums[i]
  nums[i] = nums[j]
  nums[j] = temp
}

const moveZeroes = nums => {
  let flag = true,
    lastIndex = nums.length - 1,
    swapIndex = -1

  while (flag) {
    flag = false
    for (let i = 0; i < lastIndex; i++) {
      if (nums[i] === 0) {
        swap(nums, i, i + 1)
        flag = true
        swapIndex = i
      }
    }
    lastIndex = swapIndex
  }
  return nums
}

console.log(moveZeroes([0, 1, 0, 3, 12]))
